﻿/* 
 * Copyright 2010 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace Ngilead.Core.Replicator.Clone
{
    class CloneDictionaryReplicator : DictionaryReplicator
    {
        #region Attributes

        /// <summary> Logger channel</summary>
        private static ILog logger = LogManager.GetLogger(typeof(CloneDictionaryReplicator));

        /// <summary>Persistence util class</summary>
        private IPersistenceUtil _persistenceUtil;

        #endregion

        #region Properties

        /// <summary>
        /// The associated persistence util
        /// </summary>
        public IPersistenceUtil PersistenceUtil
        {
            get { return _persistenceUtil; }
            set { _persistenceUtil = value; }
        }

        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public CloneDictionaryReplicator()
        {
        }

        #endregion

        #region Override

        /// <summary>
        /// Create a new collection for the collection argument.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="from"></param>
        /// <returns></returns>
        protected override T CreateToDictionary<T>(T from)
        {
            if (PersistenceUtil.IsPersistentDictionary(from.GetType()) == false)
            {
            //  Not a persistent collection : just call the base class
            //
                return base.CreateToDictionary<T>(from);
            }
            else
            {
            //  Just create a a new basic dictionary
            //
                Type keyType = typeof(Object);
                Type valueType = typeof(Object);
                Type dictionaryType = from.GetType();
                if (dictionaryType.IsGenericType)
                {
                    keyType = dictionaryType.GetGenericArguments()[0];
                    valueType = dictionaryType.GetGenericArguments()[1];
                }
                
                Type baseCollectionType = typeof(Dictionary<,>);
                Type genericType = baseCollectionType.MakeGenericType(keyType, valueType);
                return (T)Activator.CreateInstance(genericType);
            }
        }

        #endregion
    }
}
